//#include "../MFCApplication1/stdafx.h"
#include "pch.h"
//#include <afx.h>
#include "../sqlite3/sqlite3.h"
//#include "../log/Log.h"
#include "RegisterSqlite.h"

#define LOGFILENAME "info.log"

/*
隐藏sqlite文件，隐藏sqlite机制
*/
static void LogAppendLine(CString info)
{
	try {
		CFile f;
		f.Open(_T(LOGFILENAME), CFile::typeUnicode | CFile::modeWrite | CFile::modeCreate |
			CFile::modeNoTruncate);
		if (f.GetLength() == 0) {
			const unsigned char LeadBytes[] = { 0xff, 0xfe };
			f.Write(LeadBytes, sizeof(LeadBytes));
		}
		f.SeekToEnd();
		CString line;
		CString datetime;
		//CString str; //��ȡϵͳʱ�� ����
		CTime tm;
		tm = CTime::GetCurrentTime();
		datetime = tm.Format("%Y/%m/%d %H:%M:%S");
		line = datetime + _T("\t") + info + _T("\r\n");;
		f.Write(line, line.GetLength() * 2);
		f.Close();
	}
	catch (CFileException *e) {
		/**
		WCHAR err[255];
		e->GetErrorMessage(err, 255);
		AfxMessageBox(err);
		**/
	}
}

/*
*/
static void LogAppendLine( const char* info )
{
	FILE *f;
	fopen_s(&f, LOGFILENAME, "a+");
	fwrite(info, 1, strlen(info), f);
	fclose(f);
}

#define SQLDB "register.db"

long long RegisterMicroSecond;

/**
 * 
 * **/
static int queryCallback(void *para, int columenCount, char** columnValue, char** columnName){
  /*
    int i;
    for(i=0; i<columenCount; i++){
      printf("%s = %s\n", columnName[i], columnValue[i] ? columnValue[i] : "NULL");
    }
    printf("\n");
  */
  if(columenCount==0)
    RegisterMicroSecond = -1;
  else {	  
	  RegisterMicroSecond = atoll(columnValue[2]);
  }
  return 0;
}

//#include "log/Log.h"

/***
 * 
 * ***/
static int createTableCallback(void *para, int columenCount, char** columnValue, char** columnName ){
	return 0;
}

/***
 * 
 * ***/
static int insertCallback(void *para, int columenCount, char** columnValue, char** columnName ){
	return 0;
}

static void outputSqlError( const char* sql, const char* info ){
	char buf[255] = { '\0' };
  sprintf_s( buf, "%s, %s;", sql, info );

  LogAppendLine( buf );
}

/**
 * 
 * **/
int RegisterSqlite::createRegisterTable()
{
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  rc = sqlite3_open16( _T(SQLDB), &db);
  if( rc ){
    //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return(-1);
  }
  const char * sql = "create table register( id integer PRIMARY KEY autoincrement, hostId TEXT, registertime TEXT ); ";
  rc = sqlite3_exec(db, sql, createTableCallback, 0, &zErrMsg);

  if( rc!=SQLITE_OK ){
    //fprintf(stderr, "SQL error: %s\n", zErrMsg);
    sqlite3_free(zErrMsg);
    return -1;
  }
  sqlite3_close(db);
  return 0;
}

/***
 * 
 * */
int query()
{
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  rc = sqlite3_open16(_T(SQLDB), &db);
  if( rc ){
    //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    outputSqlError( "数据库文件打开失败", SQLDB );
    sqlite3_close(db);
    return(-1);
  }
  const char * sql = "select * from register order by id desc limit 0,1; ";
  rc = sqlite3_exec(db, sql, queryCallback, 0, &zErrMsg);

  if( rc!=SQLITE_OK ){
    //fprintf(stderr, "SQL error: %s\n", zErrMsg);
    outputSqlError( sql, zErrMsg );
    sqlite3_free(zErrMsg);
    return -1;
  }
  sqlite3_close(db);
  return 0;
}

/***
 * 
 * */
int RegisterSqlite::Register( long long _date )
{
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  rc = sqlite3_open16(_T(SQLDB), &db );
  if( rc ){
    //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    outputSqlError( "数据库文件打开失败", SQLDB );
    sqlite3_close(db);
    return(-1);
  }
  char sql[255] = { '\0' };
  sprintf_s( sql, "insert into register(registertime) values( %lld  );", _date );
  
  rc = sqlite3_exec(db, sql, insertCallback, 0, &zErrMsg);

  if( rc!=SQLITE_OK ){
    //fprintf(stderr, "SQL error: %s\n", zErrMsg);
    outputSqlError( sql, zErrMsg );
    sqlite3_free(zErrMsg);
    return -1;
  }
  sqlite3_close(db);
  return 0;
}

/**
 * 
 * ***/
int RegisterSqlite::queryRegisterDate()
{
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  rc = sqlite3_open16(_T(SQLDB), &db);
  if( rc ){
    //fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    outputSqlError( "数据库文件打开失败", SQLDB );
    sqlite3_close(db);
    return(-1);
  }
  const char * sql = "select * from register order by id desc limit 0,1; ";
  rc = sqlite3_exec(db, sql, queryCallback, 0, &zErrMsg);

  if( rc!=SQLITE_OK ){
    //fprintf(stderr, "SQL error: %s\n", zErrMsg);
    outputSqlError( sql, zErrMsg );
    sqlite3_free(zErrMsg);
    return -1;
  }
  sqlite3_close(db);
  return 0;
}

/*
*
*/
bool Expired( long long registerSecond )
{
	time_t tim;
	time(&tim);
	if (tim - registerSecond >365*3*24*3600) {
		return true;
	}
	else {
		return false;
	}
}

bool RegisterSqlite::isExpired()
{
	time_t tim;
	time(&tim);
	if (tim - RegisterMicroSecond > 365 * 3 * 24 * 3600) {
		return true;
	}
	else {
		return false;
	}
}